#include "testDrive.h"

namespace logger {

	PRecord successList;
	int successCounter;
	PRecord failList;
	int failCounter;
	PRecord exceptionList;
	int exceptionCounter;

	void init(){
		successList = NULL;
		failList = NULL;
		exceptionList = NULL;
		successCounter = 0;
		failCounter = 0;
		exceptionCounter = 0;
	}

	void pushSuccessMsg(PRecord record){
		record->next = successList;
		successList = record;
		successCounter++;
	}

	void pushFailMsg(PRecord record){
		record->next = failList;
		failList = record;
		failCounter++;
	}

	void pushExceptionMsg(PRecord record){
		record->next = exceptionList;
		exceptionList = record;
		exceptionCounter++;
	}

	PRecord createRecord(string msg, string filename, int lineNumber){
		PRecord record = (PRecord)malloc(sizeof(Record));
		record->msg = (char*)malloc(msg.size() + 1);
		record->msg[msg.size()] = 0;
		memcpy(record->msg, msg.c_str(), msg.size());
		record->filename = (char*)malloc(filename.size() + 1);
		record->filename[filename.size()] = 0;
		memcpy(record->filename, filename.c_str(), filename.size());
		record->lineNumber = lineNumber;
		record->next = NULL;
		return record;
	}

	void success(string msg, string filename, int lineNumber){
		PRecord record = createRecord(msg, filename, lineNumber);
		pushSuccessMsg(record);
	}

	void fail(string msg, string filename, int lineNumber){
		PRecord record = createRecord(msg, filename, lineNumber);
		pushFailMsg(record);
	}

	void exception(string msg, string filename, int lineNumber){
		PRecord record = createRecord(msg, filename, lineNumber);
		pushExceptionMsg(record);
	}

	void print(){
		PRecord tmp;
		ofstream out;
		out.open("output.txt");
		
		out << "Success:" << endl;
		out << "------------------------------------------------------------------" << endl;
		tmp = successList;
		while (tmp != NULL)
		{
			out << "[" << tmp->filename << " : " << tmp->lineNumber << "]\t " << tmp->msg << endl;
			tmp = tmp->next;
		}
		out << "------------------------------------------------------------------" << endl;

		out << endl << endl << "Fail:" << endl;
		out << "------------------------------------------------------------------" << endl;
		tmp = failList;
		while (tmp != NULL)
		{
			out << "[" << tmp->filename << " : " << tmp->lineNumber << "]\t " << tmp->msg << endl;
			tmp = tmp->next;
		}
		out << "------------------------------------------------------------------" << endl;

		out << endl << endl << "Exception:" << endl;
		out << "------------------------------------------------------------------" << endl;
		tmp = exceptionList;
		while (tmp != NULL)
		{
			out << "[" << tmp->filename << " : " << tmp->lineNumber << "]\t " << tmp->msg << endl;
			tmp = tmp->next;
		}
		out << "------------------------------------------------------------------" << endl;

		out << endl << endl << "Summary:" << endl;
		out << "------------------------------------------------------------------" << endl;
		out << "Success:  " << successCounter << ", Fail: " << failCounter << ", Exception: " << exceptionCounter << endl;
		out.close();
	}
}
